package adapter;

import java.util.ArrayList;

import util.DateTimeHelper;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ProgressBar;
import android.widget.TextView;
import background.DeleteTaskAsyn;
import background.FinishTaskAsyn;

import com.ntnhu2008.actionbar.R;

import entity.DummyDeleteTask;
import entity.DummyFinishTask;
import entity.FTodo;

public class FTodoExpandableAdapter extends BaseExpandableListAdapter {
	private Context mContext;
	public ArrayList<String> mGroups;
	public ArrayList<ArrayList<FTodo>> mFTodos;
	private LayoutInflater mInflater;

	public FTodoExpandableAdapter(Context context, ArrayList<String> groups,
			ArrayList<ArrayList<FTodo>> ftodos) {
		this.mContext = context;
		this.mGroups = groups;
		this.mFTodos = ftodos;
		this.mInflater = LayoutInflater.from(context);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return mFTodos.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return (long) (groupPosition * 1024 + childPosition);
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = null;
		if (convertView != null) {
			view = convertView;
		} else {
			view = mInflater.inflate(R.layout.listview_item, parent, false);
		}
					
		final FTodo ftodo = mFTodos.get(groupPosition).get(childPosition);
		Button btnPriority = (Button) view.findViewById(R.id.btnPriority);
		TextView txtTaskname = (TextView) view.findViewById(R.id.txtTaskname);
		TextView txtTime = (TextView) view.findViewById(R.id.txtTime);
		final CheckBox cbFinish = (CheckBox) view.findViewById(R.id.cbFinish);
		final TextView txtFinish = (TextView) view.findViewById(R.id.txtLineFinish);
		final ProgressBar progress = (ProgressBar) view.findViewById(R.id.progressBar);
		final Button btnDeleteTask = (Button) view.findViewById(R.id.btnDeleteTask);
		txtTaskname.setText(ftodo.taskname);
		if (ftodo.priority == 1) {
			btnPriority.setText("");
			btnPriority
					.setBackgroundResource(R.drawable.statelist_button_priority_normal);
		} else if (ftodo.priority == 2) {
			btnPriority.setText("");
			btnPriority
					.setBackgroundResource(R.drawable.statelist_button_priority_medium);
		} else if (ftodo.priority == 3) {
			btnPriority.setText("");
			btnPriority
					.setBackgroundResource(R.drawable.statelist_button_priority_hight);
		}
		if (ftodo.timeCreate != 0) {
			txtTime.setText(DateTimeHelper
					.getFormattedTime(ftodo.timeCreate));
		}
		cbFinish.setChecked(ftodo.isFinish > 0);
		cbFinish.setEnabled(!(ftodo.isFinish > 0));
		if(ftodo.isFinish > 0){
			txtFinish.setVisibility(View.VISIBLE);
			btnDeleteTask.setVisibility(View.VISIBLE);
		}else{
			txtFinish.setVisibility(View.GONE);
			btnDeleteTask.setVisibility(View.GONE);
		}
		cbFinish.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				progress.setVisibility(View.VISIBLE);
				DummyFinishTask dummy = new DummyFinishTask();
				dummy.context = mContext;
				dummy.cbFinish = cbFinish;
				dummy.txtFinish = txtFinish;
				dummy.progressBar = progress;
				dummy.btnDeleteTask = btnDeleteTask;
				dummy.ftodo = ftodo;
				new FinishTaskAsyn().execute(dummy);
				
			}
		});
		btnDeleteTask.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				DummyDeleteTask delete = new DummyDeleteTask();
				delete.context = mContext;
				delete.adapter2 = null;
				delete.adapter = FTodoExpandableAdapter.this;
				delete.childPosition = childPosition;
				delete.groupPosition = groupPosition;
				new DeleteTaskAsyn().execute(delete);
			}
		});
		return view;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return mFTodos.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return mGroups.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return mGroups.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return (long) (groupPosition * 1024);
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View v = null;
		if (convertView != null) {
			v = convertView;
		} else {
			v = mInflater.inflate(R.layout.group_row, parent, false);
		}
		String gt = (String) getGroup(groupPosition);
		TextView ftodoGroup = (TextView) v.findViewById(R.id.childname);
		if (gt != null) {
			ftodoGroup.setText(gt);
		}
		return v;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void onGroupCollapsed(int groupPosition) {
		// TODO Auto-generated method stub
		super.onGroupCollapsed(groupPosition);
	}

	@Override
	public void onGroupExpanded(int groupPosition) {
		// TODO Auto-generated method stub
		super.onGroupExpanded(groupPosition);
	}
}
